/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.tkorg.coauthorgraph.servlet.graph;

import edu.uci.ics.jung.graph.UndirectedSparseGraph;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.dom4j.Document;
import uit.tkorg.coauthorgraph.bo.CoAuthorGraphBO;
import uit.tkorg.coauthorgraph.bo.ConferenceBO;
import uit.tkorg.coauthorgraph.dto.ConfProgCommitteeGraphDTO;
import uit.tkorg.coauthorgraph.dto.InputConferenceDTO;
import uit.tkorg.coauthorgraph.utility.CommonXMLUtility;

/**
 *
 * @author Tin Huynh
 */
public class BuildPCMemberGraphServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File fileUpLoad = null;
        File destinationDir = null;

        try {
            ArrayList confCommitteeGraphDTOList = new ArrayList();
            HttpSession session = request.getSession();
            String depthLevel = "1";

            // Check that we have a file upload request
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            List /* FileItem */ items = upload.parseRequest(request);
            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String fieldName = item.getFieldName();
                    String fileName = item.getName();
                    String contentType = item.getContentType();
                    boolean isInMemory = item.isInMemory();
                    long sizeInBytes = item.getSize();
                    byte[] content = item.get();
                    String realPath = getServletContext().getRealPath("");
                    destinationDir = new File(realPath);
                    fileUpLoad = new File(destinationDir, fileName);
                    item.write(fileUpLoad);
                }
            }

            Document doc = CommonXMLUtility.readXMLFile(fileUpLoad);
            ConferenceBO confBO = new ConferenceBO();
            if (doc != null) {
                ArrayList inputConfDTOList = confBO.getInputConfFromDOMObject(doc);
                CoAuthorGraphBO coAuthorGraphBO = new CoAuthorGraphBO();
                if (inputConfDTOList != null && inputConfDTOList.size() > 0) {
                    ArrayList inputConfNameList = new ArrayList();
                    for (int i=0; i<inputConfDTOList.size(); i++) {
                        InputConferenceDTO inputConfDTO = (InputConferenceDTO) inputConfDTOList.get(i);
                        String confName = inputConfDTO.getConfName();
                        inputConfNameList.add(confName);
                    }
                    
                    for (int i=0; i<inputConfDTOList.size(); i++) {
                        InputConferenceDTO inputConfDTO = (InputConferenceDTO) inputConfDTOList.get(i);
                        ArrayList pcMemberList = inputConfDTO.getPcMemberList();
                        UndirectedSparseGraph graph = coAuthorGraphBO.buildCoAuthorGraph(pcMemberList, depthLevel, inputConfNameList);
                        ConfProgCommitteeGraphDTO confCommitteeGraphDTO = new ConfProgCommitteeGraphDTO();
                        confCommitteeGraphDTO.setConferenceName(inputConfDTO.getConfName());
                        confCommitteeGraphDTO.setGraph(graph);
                        confCommitteeGraphDTOList.add(confCommitteeGraphDTO);
                    }
                    session.setAttribute("confCommitteeGraphDTOList", confCommitteeGraphDTOList);
                    coAuthorGraphBO.saveCoAuthorGraphListToXMLFile(confCommitteeGraphDTOList);
                }
            }

            request.setAttribute("resultMessage", "Take a look at C:\\ to see the result");
            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/PCMemberGraphBuilding.jsp");
            dispatcher.forward(request, response);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
